
// 格式化时间
function formatTime(timestamp: number): string {
  const date = new Date(timestamp);
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  
  return `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day} ${hour < 10 ? '0' + hour : hour}:${minute < 10 ? '0' + minute : minute}`;
}

Page({
  data: {
    posts: [],
    userId: ''
  },

  onLoad() {
    // 获取用户ID
    const userId = wx.getStorageSync('userId') || '';
    this.setData({ userId });
  },

  onShow() {
    this.loadMyPosts();
  },

  loadMyPosts() {
    const { userId } = this.data;
    
    if (!userId) {
      this.setData({ posts: [] });
      return;
    }
    
    // 获取所有帖子
    const allPosts = wx.getStorageSync('posts') || [];
    
    // 过滤出用户自己的帖子
    let myPosts = allPosts.filter((post: any) => post.author === userId);
    
    // 格式化时间
    myPosts = myPosts.map((post: any) => {
      return {
        ...post,
        formattedTime: formatTime(post.createTime)
      };
    });
    
    this.setData({ posts: myPosts });
  },

  goToDetail(e: any) {
    const postId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/detail/detail?id=${postId}`
    });
  },

  goToPost() {
    wx.switchTab({
      url: '/pages/post/post'
    });
  }
});